package com.example.chouti;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.chouti.adapter.Card1ListAdapter;
import com.example.chouti.model.ListItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class Card1Activity extends Activity {

    String JSON_URL ="http://121.40.121.200/manhua.json";
    private JsonArrayRequest request;
    private RequestQueue requestQueue;
    RecyclerView mRecyclerview;
    List<ListItem> mListItem = new ArrayList<>();
    Card1ListAdapter card1ListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card1);
        mRecyclerview = findViewById(R.id.recyclerviewid);
        getJsonData();
    }

    private  void getJsonData() {
        request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
            JSONObject jsonObject = null;
            for (int i=0 ; i<response.length();i++){
                ListItem listItem = new ListItem();
                try {
                    jsonObject = response.getJSONObject(i);
                    listItem.setName(jsonObject.getString("name"));
                    listItem.setDescription(jsonObject.getString("description"));
                    listItem.setCategoire(jsonObject.getString("categorie"));
                    listItem.setRating(jsonObject.getString("Rating"));
                    listItem.setNb_episode(jsonObject.getInt("episode"));
                    listItem.setImg_url(jsonObject.getString("img"));
                    listItem.setStudio(jsonObject.getString("studio"));
                    mListItem.add(listItem);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.d("leueric", "onResponse: " + mListItem.size());
            card1ListAdapter = new Card1ListAdapter(getApplicationContext(),mListItem);
            mRecyclerview.setAdapter(card1ListAdapter);
            mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue = Volley.newRequestQueue(Card1Activity.this);
        requestQueue.add(request);
    }

    private  void initRecyclerView(List<ListItem> list) {

    }
}
